#include<thread>
#include<iostream>
#include<vector>

#include <pthread.h>
#include <sys/prctl.h>

void thread_fun(int i)
{
    std::string name = std::string("subthread") + std::to_string(i);
    prctl(PR_SET_NAME, reinterpret_cast<unsigned long>(name.c_str()), 0, 0, 0);

    cpu_set_t mask;
    CPU_ZERO(&mask);
    CPU_SET(i, &mask);

    if (0 != pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask))
    {
        std::cout<< name << " pthread_setaffinity_np error!" << std::endl;
        return;
    }

    std::this_thread::sleep_for(std::chrono::seconds(100));
}

int main(void)
{
    int best_thread_num = std::thread::hardware_concurrency();
    std::vector<std::thread> t;

    for (int i = 0; i < best_thread_num; i++)
    {
        t.emplace_back(std::thread(thread_fun, i));
    }
    for (int i = 0; i < best_thread_num; i++)
    {
       t[i].join();
    }
}
